<?php
/**
 * GKPHP - 极客框架
 * GK缓存模块
 */
namespace GK;
use GK\Cache\Driver;
class Cache
{
    /**
     * @var array 缓存的实例
     */
    public static $Instance = [];
    /**
     * @var int 缓存读取次数
     */
    public static $ReadTimes = 0;
    /**
     * @var int 缓存写入次数
     */
    public static $WriteTimes = 0;
    /**
     * @var object 操作句柄
     */
    public static $Handler;
    /**
     * 连接缓存驱动
     * @access public
     * @param  array       $options 配置数组
     * @param  bool|string $name    缓存连接标识 true 强制重新连接
     * @return Driver
     */
    public static function Connect(array $options = [], $name = false)
    {
        $type = !empty($options['type']) ? $options['type'] : 'File';

        if (false === $name) {
            $name = md5(serialize($options));
        }

        if (true === $name || !isset(self::$Instance[$name])) {
            $class = false === strpos($type, '\\') ?
            '\\think\\cache\\driver\\' . ucwords($type) :
            $type;

            // 记录初始化信息
            App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');

            if (true === $name) {
                return new $class($options);
            }

            self::$Instance[$name] = new $class($options);
        }

        return self::$Instance[$name];
    }
}